﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ProCodes.BugTracker.BugCatcher
{
    public static class BugDetails
    {
        public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                Exception ex = (Exception)e.ExceptionObject;
                Emails.SendEmails sendEmails = new Emails.SendEmails();
                string bugImageFile = new CaptureBugScreenShot().CaptureDesktopScreenShot();
                Emails.EmailAttachments.AttachedFiles.Add(bugImageFile);
                sendEmails.ToEmail = Properties.Settings.Default.EmailReceiver;
                sendEmails.SendingEmailType = Emails.SendEmails.EmailType.Bug;
                sendEmails.EmailBody = GetExceptionDetails(ex);
                sendEmails.ShowDialog();
            }
            finally
            {
                //No action.
            }
        }


        public static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            try
            {
                Exception ex = e.Exception;
                Emails.SendEmails sendEmails = new Emails.SendEmails();
                string bugImageFile = new CaptureBugScreenShot().CaptureDesktopScreenShot();
                Emails.EmailAttachments.AttachedFiles.Add(bugImageFile);
                sendEmails.ToEmail = Properties.Settings.Default.EmailReceiver;
                sendEmails.SendingEmailType = Emails.SendEmails.EmailType.Bug;
                sendEmails.EmailBody = GetExceptionDetails(ex);
                sendEmails.ShowDialog();
            }
            finally
            {
                //No action
            }
        }
        public static string GetExceptionDetails(Exception ex)
        {
            string exceptionData = String.Empty;
            foreach (DictionaryEntry keyValuePair in ex.Data)
            {
                exceptionData += keyValuePair.Key.ToString() + ":" + keyValuePair.Value.ToString() + Environment.NewLine;
            }

            string exceptionDetails = Properties.Settings.Default.BugEmailBody
            .Replace("{receiver}", "Ahmed Mohammed Fahmy")
            .Replace("{nl}", Environment.NewLine)
            .Replace("{emailAdress}", PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.EMPLOYEE_EMAIL)
            .Replace("{employeeName}", PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.EMPLOYEE_FULL_NAME)
            .Replace("{employeeMobile}", "Mobile Number : ")
            .Replace("{ExceptionMessage}", ex.Message)
            .Replace("{Source}", ex.Source)
            .Replace("{TargetSite}", ex.TargetSite.ToString())
            .Replace("{StackTrace}", ex.StackTrace)
            .Replace("{Data}", exceptionData);
            return exceptionDetails;
        }
    }
}
